<template>
  <el-container>
    <my-theme-card>
      <el-header>
        <el-row class="header-row">
          节点名称:
          <el-input v-model="conditions[0].value" placeholder="请输入内容" style="width:180px;" />
          流程编号:
          <el-input v-model="conditions[1].value" placeholder="请输入内容" style="width:180px;" readonly @focus="chooseProcessClick" />
          <el-button class="table-header-btn" type="primary" icon="el-icon-search" @click="search">查询</el-button>
        </el-row>
      </el-header>
      <el-main>
        <el-col :span="1"><div class="grid-content bg-purple-light">
          <el-button class="table-header-btn" type="primary" icon="el-icon-plus" size="mini" @click="addClick">新增</el-button>
        </div></el-col>
        <template>
          <el-table :data="taskList" stripe border style="width: 100%">
            <el-table-column prop="nodesn" label="节点号" width="180" />
            <el-table-column prop="procno" label="流程编号" />
            <el-table-column prop="nodename" label="节点名称" width="180" />
            <el-table-column label="自动提交" width="180">
              <template slot-scope="scope">
                {{ key_logic[scope.row.auto_submit] }}
              </template>
            </el-table-column>
            <el-table-column label="意见是否必须" width="180">
              <template slot-scope="scope">
                {{ key_logic[scope.row.is_opinion] }}
              </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="250">key_logic
              <template slot-scope="scope">
                <el-button type="text" size="small" @click="deleteClick(scope.row)">删除</el-button>
                <el-button type="text" size="small" @click="setProcNodeClick(scope.row)">节点设置</el-button>
                <el-button type="text" size="small" @click="editClick(scope.row)">编辑</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-main>

      <el-footer>
        <div class="block">
          <el-pagination
            :current-page="page.current"
            :page-sizes="[5,10, 20, 50, 100]"
            :page-size="page.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-footer>

      <my-delete-dialog
        :show-dialog="dialogConfirmVisible"
        :row="currentJsonDictionaryTemp"
        @deleteTask="deleteTask"
        @cancelTask="dialogConfirmVisible = false"
      />

      <el-dialog title="流程选择" :visible.sync="dialogProcessVisible" :close-on-click-modal="false" center :custom-class="dialog1" append-to-body>
        <process-list @processListClose="closeProcessDialog" />
      </el-dialog>
    </my-theme-card>
  </el-container>
</template>

<style>
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>

<script>
import request from '@/utils/request'
import processList from '@/components/ProcessList/index'
import MyThemeCard from '../../../../components/MyThemeCard/index'

export default {
  components: {
    MyThemeCard,
    processList
  },
  props: {
    procno: String
  },
  data: function() {
    return {
      dialog1: 'dialog1',
      conditions: [
        {
          field: 'nodename',
          type: 'STRING',
          value: '',
          symbol: 'LIKE'
        },
        {
          field: 'procno',
          type: 'STRING',
          value: '',
          symbol: 'LIKE'
        }
      ],
      orders: [
        { field: 'nodesn' }
      ],
      baseUrl: process.env.VUE_APP_BASE_API,
      page: {
        current: 1,
        size: 5,
        total: 0
      },
      taskList: [],
      currentJsonDictionaryTemp: {},
      dialogProcessVisible: false,
      dialogConfirmVisible: false
    }
  },
  computed: {
    key_logic: function() {
      const { logic = {}} = this.$store.state.directory.directoryItem
      return logic
    },
    key_tskstat: function() {
      const { tskstat = {}} = this.$store.state.directory.directoryItem
      return tskstat
    },
    dir_tskstat: function() {
      return this.$store.state.directory.directory['tskstat']
    }
  },
  created() {
    if (this.procno) {
      this.conditions[1].value = this.procno
    }
    this.directoryUpdate()
    this.query()
  },
  methods: {
    // 流程选择点击事件
    chooseProcessClick: function() {
      this.dialogProcessVisible = true
    },
    // 关闭流程弹框时
    closeProcessDialog: function(ids, objs) {
      if (objs) {
        objs.forEach((obj) => {
          this.conditions[1].value = obj.procno
        })
      }
      this.dialogProcessVisible = false
    },
    // 字典更新
    directoryUpdate: function() {
      this.$store.commit('directory/setDirectory', 'logic')
      this.$store.commit('directory/setDirectory', 'tskstat')
    },
    // 编辑事件
    editClick: function(row) {
      this.$emit('toChangePage', 'edit', row)
    },
    // 节点信息设置
    setProcNodeClick: function(row) {
      this.$emit('toChangePage', 'node_info_set', row)
    },
    // 添加节点点击事件
    addClick: function() {
      this.$emit('toChangePage', 'add')
    },
    deleteClick: function(row) {
      this.currentJsonDictionaryTemp = row
      this.dialogConfirmVisible = true
    },
    deleteTask: function() {
      this.$progress.start()
      request({
        url: '/task/node/delete',
        method: 'post',
        data: this.currentJsonDictionaryTemp
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.dialogConfirmVisible = false
          this.query()
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { this.$progress.done() })
    },
    // 每页显示数目发生变化时
    handleSizeChange: function(val) {
      this.page.size = val
      this.page.current = 1
      this.query()
    },
    // 当前页发生变化时
    handleCurrentChange: function(val) {
      this.page.current = val
      this.query()
    },
    // 搜索
    search: function() {
      this.page.current = 1
      this.query()
    },
    query: function() {
      this.$progress.start()
      this.page.conditions = this.conditions
      this.page.orders = this.orders
      request({
        url: '/task/node/query',
        method: 'post',
        data: this.page
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.page.total = res.data.total
          this.taskList = res.data.data
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { this.$progress.done() })
    }
  }
}
</script>
